﻿using Microsoft.AspNetCore.Mvc;
using orm.freesql.api.Entities;

namespace orm.freesql.api.Controllers
{
    /// <summary>
    /// 进阶操作
    /// </summary>
    public class AdvanceController(IFreeSql freeSql) : FreeSqlBaseController(freeSql)
    {
        /// <summary>
        /// 事务
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Transaction()
        {
            var order = new Order
            {
                Number = DateTime.Now.ToString("yyyyMMddHHmmss"),
                TotalPrice = 1,
                CreateTime = DateTime.Now
            };

            var order2 = new Order
            {
                Number = DateTime.Now.ToString("yyyyMMddHHmmss"),
                TotalPrice = 2,
                CreateTime = DateTime.Now
            };

            using var uow = FreeSql.CreateUnitOfWork();

            await uow.Orm.Insert(order).ExecuteIdentityAsync();
            await uow.Orm.Insert(order2).ExecuteIdentityAsync();

            uow.Commit();

            return Ok("Success");
        }
    }
}
